package com.viphuli.app.tool.handler;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.viphuli.app.tool.bean.page.ArrangeActionPage;
import com.viphuli.app.tool.bean.part.ArrangeInfo;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.fragment.ArrangeActionTypeFragment;
import com.viphuli.app.tool.fragment.ArrangeViewWeekFragment;

public class ArrangeActionResponseHandler extends MyBaseHttpResponseHandler<ArrangeActionTypeFragment, ArrangeActionPage> {

	@Override
	public void deal() {

		//获得操作结果
		ArrangeInfo arrangeInfo = page.getArrangeInfo();

		List<Integer> checkList = caller.getAdapter().getCheck();
		List<ArrangeType> arrangeTypeList = new ArrayList<ArrangeType>();

		for (int check : checkList) {

			ArrangeType arrangeType = caller.getList().get(check);
			arrangeTypeList.add(arrangeType);

		}

		//得到宿主
		ArrangeViewWeekFragment frag = (ArrangeViewWeekFragment) caller.getParentFragment();
		
		ViewGroup vg = (ViewGroup)frag.getClickView().getParent();
		vg.getChildAt(1).setVisibility(View.GONE);

		//更新UI
		if (arrangeTypeList.size() == 1) {

			TextView text = (TextView) frag.getClickView().getChildAt(0);
			text.setTextColor(Color.parseColor(arrangeTypeList.get(0).getColor()));
			text.setText(arrangeTypeList.get(0).getName());
			
			text.setVisibility(View.VISIBLE);
			
		} else if (arrangeTypeList.size() == 2) {

			TextView textOne = (TextView) frag.getClickView().getChildAt(0);
			TextView textTwo = (TextView) frag.getClickView().getChildAt(1);

			textOne.setTextColor(Color.parseColor(arrangeTypeList.get(0).getColor()));
			textOne.setText(arrangeTypeList.get(0).getName());

			textTwo.setTextColor(Color.parseColor(arrangeTypeList.get(1).getColor()));
			textTwo.setText(arrangeTypeList.get(1).getName());
			
			textOne.setVisibility(View.VISIBLE);
			textTwo.setVisibility(View.VISIBLE);
			
			vg.getChildAt(1).setVisibility(View.VISIBLE);
		}

		//操作结果记录到宿主
		frag.getArrangeIds().add(arrangeInfo.getId());

		caller.setReqIndex(caller.getReqIndex() + 1);

		//当排班ID为2个时，关闭对话框
		if (caller.getDialog() != null && caller.getReqIndex() == caller.getCheckList().size())
			caller.getDialog().dismiss();

	}
}
